#include <gtk/gtk.h>
#include <iostream>
#include "area.h"
#include <curl/curl.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
GtkWidget *user_entry, *pwd_entry;
const gchar *g_basenumber, *g_password;

size_t write_null(void *ptr, size_t size, size_t nmemb, void *stream)
{	size_t nm = nmemb;
	return nm;
}

void scan_account()
{
  g_basenumber=gtk_entry_get_text(GTK_ENTRY(user_entry));
  g_password=gtk_entry_get_text(GTK_ENTRY(pwd_entry));
  int i = 0;
  FILE *fp;
  URL wlanUrl;
  string paramStr, wifiurl;
  wlanUrl = init_region();
  paramStr = get_paramStr();
  CURL *curl;
  CURLcode res;
  string basenumber = g_basenumber;
  string password = g_password;
  string username;
  char str[10];
/* 
  while(true) { 
    sprintf(str, "%03ld",random()%1000);
    username = basenumber + str;
    wifiurl = get_url(username,password,paramStr,wlanUrl.cwUrlRegion.js);
    curl = curl_easy_init();
    cout << username << endl;
    if(curl) {
      curl_easy_setopt(curl, CURLOPT_URL, wifiurl.c_str());
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_null);
      curl_easy_perform(curl);
      /* always cleanup */
 /*     curl_easy_cleanup(curl);
    }
    
    cout << i++ << " times" << endl;
    if(check_state()){
      break;
    }
  }
*/
}

void login_clicked(GtkButton *button, gpointer user_data){
	scan_account();
}

void exit_clicked(GtkButton *button, gpointer user_data){
	gtk_main_quit();
}

int main(int argc, char **argv)
{  
  GtkWidget *main_window,*info_table;
  GtkWidget *login_button, *exit_button;
  GtkWidget *user_label, *pwd_label;

  /* Init Wlan login window */
  gtk_init(&argc, &argv);
  main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(main_window), "Chinanet Login");
  gtk_widget_set_size_request(main_window,250, 300);
  gtk_container_set_border_width (GTK_CONTAINER (main_window), 10);
  gtk_window_set_resizable(GTK_WINDOW(main_window), FALSE);
  
  user_label = gtk_label_new ("Username:");
  pwd_label = gtk_label_new ("Password:");
  user_entry = gtk_entry_new();
  pwd_entry = gtk_entry_new(); 
  login_button = gtk_button_new_with_label("Login");
  exit_button = gtk_button_new_with_label("exit");
  gtk_widget_set_size_request(login_button, 50,30);
  gtk_widget_set_size_request(exit_button, 50,30);
  
  g_signal_connect(G_OBJECT(login_button), "clicked",
		   G_CALLBACK(login_clicked), NULL);
  g_signal_connect(G_OBJECT(exit_button), "clicked",
		   G_CALLBACK(exit_clicked), NULL);

  info_table = gtk_table_new (5,2,FALSE);
  gtk_table_attach(GTK_TABLE(info_table),user_label,0, 1, 0,1,
					GTK_EXPAND, GTK_SHRINK,0,0);
  gtk_table_attach(GTK_TABLE(info_table),pwd_label,0, 1, 1, 2,
					GTK_EXPAND, GTK_SHRINK,0,0);
  gtk_table_attach(GTK_TABLE(info_table),user_entry,1, 2, 0,1,
					GTK_EXPAND, GTK_SHRINK,0,0);
  gtk_table_attach(GTK_TABLE(info_table),pwd_entry,1, 2, 1, 2,
					GTK_EXPAND, GTK_SHRINK,0,0);
  gtk_table_attach(GTK_TABLE(info_table),login_button,0, 1, 3,4,
					GTK_EXPAND, GTK_SHRINK,0,0);
  gtk_table_attach(GTK_TABLE(info_table),exit_button,1, 2, 3, 4,
					GTK_EXPAND, GTK_SHRINK,0,0);
  gtk_container_add (GTK_CONTAINER (main_window), info_table);
  
  gtk_table_set_col_spacings(GTK_TABLE(info_table),5);
  gtk_table_set_row_spacings(GTK_TABLE(info_table),5);
  
  /* Show all Widget */
  gtk_widget_show_all(main_window);
  gtk_main();

  return 0;
}
